package com.gitee.huanminabc.shiro.controller;

import com.gitee.huanminabc.shiro.service.UserService;
import com.gitee.huanminabc.shiro.entity.UserEntity;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Api(tags = "UserShiroController")
@RestController
@RequestMapping("/admin")
@CrossOrigin
@Slf4j
public class UserShiroController {
    @Autowired
    UserService userService;

    //去登录页面
    @RequestMapping("/toLogin")
    public ModelAndView loginPage() {
        return new ModelAndView("login");
    }

    //登录
    @RequestMapping("/login")
    public ModelAndView login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Model model) {
        System.out.println("登录...............................\n\n");
        System.out.println("登录用户名：" + username);
        System.out.println("登录密码：" + password);
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户的登录数据,在UserRealm中进行认证
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            subject.login(token);//执行登录的方法，如果没有异常就说明ok
            ModelAndView index = new ModelAndView("index");
            index.addObject("message", "登录成功！");
            return index;
        } catch (UnknownAccountException e) {     //用户名不存在
            ModelAndView login = new ModelAndView("login");
            login.addObject("message", "用户名错误！");
            return login;
        } catch (IncorrectCredentialsException e) {//密码错误
            ModelAndView login = new ModelAndView("login");
            login.addObject("message", "密码错误！");
            return login;
        }catch (AuthorizationException e) {
            ModelAndView login = new ModelAndView("login");
            login.addObject("message", "没有权限！");
            return login;
        }
    }


    @RequestMapping("/error")
    public String error() {
        return "error";
    }

    @RequiresPermissions("query")
    @GetMapping("/index")
    public String index() {
        return "index success!";
    }






    //必须登录才能访问
    @GetMapping("/add")
    public String add() {
        return "add success!";
    }



    @RequiresRoles("test") //必须有test角色才能访问
    @GetMapping("/role")
    public String role() {
        Subject subject = SecurityUtils.getSubject();
        UserEntity principal = (UserEntity) subject.getPrincipal();
        System.out.println("principal = " + principal);
        boolean test = subject.hasRole("test");
        System.out.println("test = " + test);
        return "role success!";
    }

    @RequiresPermissions("perm") //必须有perm权限才能访问
    @GetMapping("/perm")
    public String perm() {
        return "Perm success!";
    }




}
